//
//  NBFatherController.m
//  聚超值
//
//  Created by 周abel on 16/4/1.
//  Copyright © 2016年 Zhli. All rights reserved.
//

#import "NBFatherController.h"
#import "NBWebVC.h"

#define basePath @"http://mrobot.pconline.com.cn/s-1800/best/cms/topicDetail.xsp?topicId="


@interface NBFatherController ()

//@property (nonatomic, strong)NSMutableArray *refresh_pull;

@property (nonatomic, strong)NSMutableArray *refresh_loading;


@end

@implementation NBFatherController

- (NSMutableArray *)refresh_loading {
    
    if (_refresh_loading == nil) {
        _refresh_loading = [NSMutableArray array];
        for (int i = 1; i<=10; i++) {
            NSString *imageName = [NSString stringWithFormat:@"refresh_loading_%zd", i];
            UIImage *image = [UIImage imageNamed:imageName];
            [_refresh_loading addObject:image];
        }
    }
    return _refresh_loading;
}

#warning mark -请勿删除, 未来使用
//-(NSMutableArray *)refresh_pull {
//    
//    if (_refresh_pull == nil) {
//        _refresh_pull = [NSMutableArray array];
//        for (int i = 1; i<=16; i++) {
//            NSString *imageName = [NSString stringWithFormat:@"refresh_pull_%zd", i];
//            UIImage *image = [UIImage imageNamed:imageName];
//            [_refresh_pull addObject:image];
//        }
//    }
//    return _refresh_pull;
//}

- (NSMutableArray *)itemArr {
    
    if (_itemArr == nil) {
        _itemArr = [NSMutableArray array];
    }
    return _itemArr;
}

- (NSMutableArray *)itemArr_2 {
    
    if (_itemArr_2 == nil) {
        _itemArr_2 = [NSMutableArray array];
    }
    return _itemArr_2;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 注册nib
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([NBCell class]) bundle:nil] forCellReuseIdentifier:@"cell_1"];
    
    // 3.0 刷新数据
    [self headeRefresh];
    [self footerRefresh];
    
    // 马上进入刷新状态
    [self.tableView.mj_header beginRefreshing];
    
}

#pragma mark -刷新数据
// 下拉刷新
- (void)headeRefresh {
    
    MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerRefresh)];
    
    // 设置即将刷新状态的动画图片（一松开就会刷新的状态）
    [header setImages:self.refresh_loading forState:MJRefreshStatePulling];
    
    // 设置正在刷新状态的动画图片
    [header setImages:self.refresh_loading forState:MJRefreshStateRefreshing];
    
    // 隐藏时间
    header.lastUpdatedTimeLabel.hidden = YES;
    
    // 隐藏状态
    header.stateLabel.hidden = YES;
    
    // 设置header
    self.tableView.mj_header = header;
}
// 上拉刷新
- (void)footerRefresh {
    
    MJRefreshAutoGifFooter *footer = [MJRefreshAutoGifFooter footerWithRefreshingTarget:self refreshingAction:@selector(sendRequest)];
    
    // 设置文字
    [footer setTitle:@"正在努力加载中..." forState:MJRefreshStateRefreshing];
    [footer setTitle:@"数据全部加载完毕" forState:MJRefreshStateNoMoreData];
    
    // 设置字体
    footer.stateLabel.font = [UIFont systemFontOfSize:14];
    
    // 设置尾部
    self.tableView.mj_footer = footer;
}

// 结束刷新
- (void)endRefresh {
    
    [self.tableView.mj_header endRefreshing];
    [self.tableView.mj_footer endRefreshing];
}
#pragma mark -单独处理上拉刷新
- (void)headerRefresh {

    self.page = 0;
    self.itemArr = nil;
    self.itemArr_2 = nil;
    
    [self performSelector:@selector(sendRequest)];

}
#pragma mark -tableView数据源方法
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.itemArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    // 1.0获取cell
    NBCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell_1"];
    
    tableView.rowHeight = 120;
    
    if (self.itemArr.count) {
        // 获取对应的item
        NBItem *item = self.itemArr[indexPath.row];
        // 2.0 设置数据
        cell.item = item;
    }
    
    // 3.0 返回cell
    return cell;
}

#pragma mark -监听滚动
// 开始滚动
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    // 代理方法
    if ([self.delegate respondsToSelector:@selector(selected:Distance:)]) {
        [self.delegate selected:self Distance:scrollView.contentOffset.y];
    }
}

#pragma mark -监听点击跳转页面
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    // 1.0 获取当前对应的模型
    NBItem *item = self.itemArr[indexPath.row];
    
    // 2.0 拼接路径
    NSString *path = [NSString stringWithFormat:@"%@%@", basePath, item.topicId];
    
    // 3.0 跳转到webVC
    NBWebVC *webVC = [[NBWebVC alloc] init];
    webVC.url = [NSURL URLWithString:path];
    
    [self.navigationController pushViewController:webVC animated:YES];
    
    
}


@end
